package cn.wit.front.service.impl;

import cn.wit.front.condition.BaoyanCondition;
import cn.wit.front.mapper.BaoyanMapper;
import cn.wit.front.service.BaoyanService;
import cn.wit.utils.pojo.Baoyan;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BaoyanServiceImpl implements BaoyanService {
    @Autowired
    private BaoyanMapper baoyanMapper;


    @Override
    public Result searchBaoyan(IPage<Baoyan> page, BaoyanCondition condition) {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(date);
        QueryWrapper<Baoyan> wrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(condition.getCategory())) {
            wrapper.like("category", condition.getCategory());
        }
        if (!StringUtils.isBlank(condition.getSchool())) {
            wrapper.like("school", condition.getSchool());
        }
        if (!StringUtils.isBlank(condition.getInstitute())) {
            wrapper.like("institute", condition.getInstitute());
        }
        if(condition.getState()!=null){
            if(condition.getState().equals("1")){
                //报名未开始
                wrapper.gt("entry_begin_time",now);
            }else if(condition.getState().equals("2")){
                //报名中
                wrapper.le("entry_begin_time",now);
                wrapper.gt("entry_end_time",now);
            }else if(condition.getState().equals("3")){
                //报名结束 但未开始活动
                wrapper.le("entry_end_time",now);
                wrapper.gt("begin_time",now);
            }else if(condition.getState().equals("4")){
                //活动中但未结束
                wrapper.le("begin_time",now);
                wrapper.gt("end_time",now);
            }else if(condition.getState().equals("5")){
                wrapper.le("end_time",now);
            }
        }
        wrapper.eq("1", "1");
        IPage<Baoyan> baoyanIPage = baoyanMapper.selectPage(page, wrapper);
        return Result.ok(baoyanIPage);
    }
}
